/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','observe_questionaire');

/*controller部分*/

//主要操作controller
authApp.controller("downcentreList",function($scope,$http,Upload,$timeout,$sce){
	//针对此页面需要的参数 should edit
	$scope.isAddM=true;
	$scope.isUrl=true;
	$scope.isSel01=true;
	$scope.isSel02=true;
	$scope.uploadFile=true;
	$scope.isHExpert=true;
	$scope.isclickBtn=false;
	$scope.isDownload=true;
	//**********主体方法
	$scope.ifFirstTimeQuery=true;
	$scope.type01=1;
	var queryMainApi=apis.queryQuestionnaireByObservation;
	$scope.queryMain=function(){
		$http({
			method:'get',
			url:queryMainApi,
			params:{},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		if(response.code=="000000"){
			$scope.account=response.data.account;
			$scope.schoolName=response.data.schoolName;
			$scope.studentFileName=response.data.studentFileName;
			$scope.teacherFileName=response.data.teacherFileName;
			$scope.employerFileName=response.data.employerFileName;
			$scope.pid=response.data.pid;
			
			if(response.data.major=="0"){
				$scope.major="学前教育 ";
			}else if(response.data.major=="1"){
				$scope.major="小学教育";
			}else if(response.data.major=="2"){
				$scope.major="中学教育";
			}
			if(response.data.overallSatisfaction=="0"){
				$scope.overallSatisfaction="不满意 ";
			}else if(response.data.overallSatisfaction=="1"){
				$scope.overallSatisfaction="一般般";
			}else if(response.data.overallSatisfaction=="2"){
				$scope.overallSatisfaction="很满意";
			}else{
				$scope.overallSatisfaction="未选择";
			}
		}else{
			alert(response.errorMsg);
		}
	};
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	
	$scope.uploadFiles= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadTaskAuthQuestionnaire,
                data: {file: file}
            });

            file.upload.then(function (response) {            	
                $timeout(function () {               	
                	if(response.data.code=="000000"){
                		//alert(index);
                		if(index=='1')
                			$scope.studentFileName=response.data.data;
                		if(index=='3')
                			$scope.employerFileName=response.data.data;
                		if(index=='2')
                			$scope.teacherFileName=response.data.data;
                		/*$scope.teacherFileName=file.teacherFileName;
                		$scope.employerFileName=file.employerFileName;
                		$scope.uploadFile=response.data.data;*/
                	}
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                evt.loaded / evt.total));
            });
        }
    }
	
	$scope.PID="";
	$scope.studentFileName01="";
	$scope.teacherFileName01="";
	$scope.employerFileName01="";
	$scope.openDownLoad=function(pid,studentFileName,teacherFileName,employerFileName){
		$scope.isDownload=false;
		$scope.studentFileName01=studentFileName;
		$scope.teacherFileName01=teacherFileName;
		$scope.employerFileName01=employerFileName;
		$scope.PID=pid;
	}
	
	$scope.cancelDownload=function(){
		$scope.isDownload=true;
	}
	
	//学生下载
	$scope.turnToDownLoad=function(type){
		if(window.confirm("是否确定下载 ？")){
			if("0"==type){			
				location.href=apis.downloadFileQuestionnaire3+"?type=0&pid="+$scope.PID;
			}else if("1"==type){
				location.href=apis.downloadFileQuestionnaire3+"?type=1&pid="+$scope.PID;
			}else if("2"==type){
				location.href=apis.downloadFileQuestionnaire3+"?type=2&pid="+$scope.PID;
			}	
		}
	}
	
	/*//教师下载
	$scope.turnToDownLoad1=function(pid,type){
		if(window.confirm("是否确定下载 ？")){
			alert("1");
			location.href=apis.downloadFileQuestionnaire+"?type=1&pid="+pid;
		}
	}
	
	//单位下载
	$scope.turnToDownLoad2=function(pid,type){
		if(window.confirm("是否确定下载 ？")){
			alert("2");
			location.href=apis.downloadFileQuestionnaire+"?type=2&pid="+pid;
		}
	}*/
	
	
	
	
	$scope.myFunc = function(){
		$scope.type01="";
		//$scope.name01="";
		if($scope.sel=="0"){
			$scope.isSel01=false;
			$scope.isSel02=true;
		}else if($scope.sel=="1"){
			$scope.isSel01=true;
			$scope.isSel02=false;			
		}else{
			$scope.isSel01=true;
			$scope.isSel02=true;
		}
	}
	
	$scope.showUrl=function(){
		if($scope.type=="2"){
			$scope.isUrl=false;
		}else{
			$scope.isUrl=true;
		}
	}
	 //按钮添加事件
	$scope.addM=function(){
		$scope.isAddM=false;
		$scope.isclickBtn=false;
	}
	//按钮取消事件
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.clearInfo();
	}
	$scope.clearInfo=function(){
		$scope.pid="";
		$scope.account="";
		$scope.schoolName="";
		$scope.major="";
		$scope.overallSatisfaction="";
	}
	
	
	$scope.update=function(){
		alert("xxxxxxx");
		if(window.confirm("是否确认修改？")){	
			var rJson={
					'pid':$scope.pid,
					'studentFileName':$scope.studentFileName,
					'studentFileUrl':$scope.studentFileUrl,
					'teacherFileName':$scope.teacherFileName,
					'teacherFileUrl':$scope.teacherFileUrl,
					'employerFileName':$scope.employerFileName,
					'employerFileUrl':$scope.employerFileUrl,
					'overallSatisfaction':$scope.overallSatisfaction
					};
			$http({
				method:'post',
				url:apis.saveAuthQueDownloadInfo,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
					alert("修改成功！");
					$scope.pid="";
					$scope.studentFileName="";
					$scope.studentFileUrl="";
					$scope.teacherFileName="";
					$scope.teacherFileUrl="";
					$scope.employerFileName="";
					$scope.employerFileUrl="";
					$scope.overallSatisfaction="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}
	}
	
	$scope.openAddM=function(pid){
		alert(pid);
	}
	//根据id查询调查问卷
	$scope.queryAuthQuestionnaireInfoBypid=function(pid){		
		$scope.isAddM=false;
		$scope.isclickBtn=true;
		$scope.Id=pid;
		var random=Math.random();
		$http({
			method:'get',
			url:apis.queryAuthQuestionnaireInfoBypid,
			params:{'pid':pid,'random':random}
		}).success(function(response){
			if(response.code=="000000"){
				$scope.pid=response.data.pid;
				$scope.major=response.data.major;					
				$scope.schoolName=response.data.schoolName;
				$scope.studentFileName=response.data.studentFileName;
				//$scope.studentFileUrl=response.data.studentFileUrl;
				$scope.teacherFileName=response.data.teacherFileName;
				//$scope.teacherFileUrl=response.data.teacherFileUrl;
				$scope.employerFileName=response.data.employerFileName;
				//$scope.employerFileUrl=response.data.employerFileUrl;
				$scope.overallSatisfaction=response.data.overallSatisfaction;
				
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
			$scope.isAddM=true;
		})
	}
	
	
})